package learnjava.basic.other;

import java.util.ArrayList;
import java.util.Iterator;

public class TestGeneric {

    public static void main(String[] args) {

//        Utils<Person> utils = new Utils<>();
//        utils.setT(new Teacher());
//        Person teacher = utils.getT();
//        utils.print("s2");

        //
    }
}

class Utils<T>{
    private T t;

    public T getT() {
        return t;
    }

    public void setT(T t) {
        this.t = t;
    }

    //如果方法使用的泛型和类的泛型不同，可以将泛型声明在方法上
    public <Q> void print(Q q){
        System.out.println();
    }

    //无限定泛型，其实和上面在方法上声明新的泛型效果一样
    public static void printColl1(ArrayList<?> al){
        Iterator<?> it = al.iterator();
        while (it.hasNext()) System.out.println(it.next());
    }

    //泛型上限定
    public static void printColl2(ArrayList<? extends Person> al){
        Iterator<? extends Person> it = al.iterator();
        while (it.hasNext()) System.out.println(it.next());
    }

    //泛型下限定
    public static void printColl3(ArrayList<? super Teacher> al){
        Iterator<? super Teacher> it = al.iterator();
        while (it.hasNext()) System.out.println(it.next());
    }
}

class Teacher extends Person{

}